package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 在Test2需求的基础上，要求调用@AutoRunMethod标注的方法若干次
 * 次数由注解参数值决定
 *
 * https://gitee.com/xiloer/jsd2409-ssm-teacher.git
 */
public class Test3 {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = scanner.nextLine();

        Class cls = Class.forName(className);
        if(cls.isAnnotationPresent(AutoRunClass.class)){
            Object obj = cls.newInstance();//实例化对象
            //获取该类定义的所有方法
            Method[] methods = cls.getDeclaredMethods();
            for(Method method : methods){
                if(method.isAnnotationPresent(AutoRunMethod.class)){
                    AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                    int value = arm.value();//获取参数value的值
                    System.out.println("调用方法:"+method.getName()+"()"+value+"次");
                    for (int i = 0; i < value; i++) {
                        method.invoke(obj);
                    }
                }
            }

        }else{
            System.out.println(className+":没有被注解@AutoRunClass标注");
        }
    }
}
